<template>
  <div class="cs-page-container">
    <el-tabs value="first" type="border-card" @tab-click="handleClick">
      <el-tab-pane label="提现列表" name="first" />
      <el-tab-pane label="已提现列表" name="second" />
      <el-tab-pane label="拒绝提现列表" name="third" />

      <div class="cs-filter-container">
        <el-input
          v-model="listQuery.userId"
          clearable
          placeholder="请输入用户ID"
        />
        <el-select
          v-model="listQuery.withdrawType"
          clearable
          placeholder="提现方式"
          style="width:100px"
        >
          <el-option
            v-for="(option, key , index) in payTypeItem"
            :key="index"
            :label="option"
            :value="key"
          />
        </el-select>
        <el-select
          v-model="listQuery.countryCode"
          clearable
          filterable
          placeholder="国家"
          style="width:100px"
        >
          <el-option
            v-for="(value, key, index) in countryCodeItem"
            :key="index"
            :label="value"
            :value="key"
          />
        </el-select>
        <el-date-picker
          v-model="daterangeValue"
          :picker-options="daterangePickerOptions"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="创建起始时间"
          end-placeholder="创建结束时间"
          value-format="yyyy/MM/dd"
          @change="daterangeChanged"
        />
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="handleFilter"
        >搜索</el-button>
        <el-button
          type="info"
          size="small"
          icon="el-icon-refresh-right"
          @click="handleResetFilter"
        >重置</el-button>
        <span>批量操作:</span>
        <el-select v-model="value" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" size="small" @click="handleMutiple">提交</el-button>
      </div>
      <div class="cs-filter-result">
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          :height="'calc(100vh - 300px)'"
          border
          fit
          highlight-current-row
          style="width: 100%;"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column
            label="提现日期"
            prop="applyTime"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.applyTime | parseTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="最后操作时间"
            prop="updateTime"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.updateTime | parseTime }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="ID"
            prop="applyTime"
            align="center"
            width="100"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.withDrawId }}</span>
            </template>
          </el-table-column>
          <el-table-column label="用户ID" align="center">
            <template slot-scope="{ row }">
              <TooptipsCom :user-id="row.userId" />
            </template>
          </el-table-column>
          <el-table-column label="用户昵称" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.nickname }}</span>
            </template>
          </el-table-column>

          <el-table-column label="认证状态" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.identifyType | identify }}</span>
            </template>
          </el-table-column>
          <el-table-column label="封禁状态" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.forbiddenState | forbidden }}</span>
            </template>
          </el-table-column>
          <el-table-column label="点播视频违规(近三天)" align="center" width="130">
            <template v-if="scope.row.illegalInfo" slot-scope="scope">
              <span>违规次数: {{ scope.row.illegalInfo.demand }}<br>监控开启次数: {{ scope.row.illegalInfo.demandMonitoring }}<br>监控关闭次数: {{ scope.row.illegalInfo.demand - scope.row.illegalInfo.demandMonitoring }}<br></span>
            </template>
          </el-table-column>
          <el-table-column label="随机视频违规(近三天)" align="center" width="130">
            <template v-if="scope.row.illegalInfo" slot-scope="scope">
              <span>违规次数: {{ scope.row.illegalInfo.random }}<br>监控开启次数: {{ scope.row.illegalInfo.randomMonitoring }}<br>监控关闭次数: {{ scope.row.illegalInfo.random - scope.row.illegalInfo.randomMonitoring }}<br></span>
            </template>
          </el-table-column>
          <el-table-column label="提现方式" align="center">
            <template slot-scope="scope">
              <span>{{ payType[scope.row.type] }}</span>
            </template>
          </el-table-column>
          <el-table-column label="累计提现金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.totalWithdrawAmount / 10000 }}</span>
            </template>
          </el-table-column>
          <el-table-column label="本次提现金额" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.amount / 10000 }} {{ payAmount[scope.row.type] }}</span>
            </template>
          </el-table-column>
          <el-table-column label="当前状态" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.status | filterType }}</span>
            </template>
          </el-table-column>
          <el-table-column label="处理原因" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.dealReason }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="国家" width="130">
            <template slot-scope="{ row }">{{ row.countryCode | countryCode }}</template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="280">
            <template slot-scope="scope">
              <el-button type="danger" @click="showDialog(scope.row)">拒绝支付</el-button>
              <el-button
                type="primary"
                @click="handleApply(scope.row, true, '')"
              >允许支付</el-button>
            </template>
          </el-table-column>
        </el-table>
        <TablePagination
          :current-page="listQuery.page"
          :limit="listQuery.limit"
          :total="total"
          :loading="listLoading"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
        />
        <el-dialog
          v-loading="dialogLoading"
          title="拒绝支付"
          :visible.sync="dialogVisible"
          width="50%"
        >
          <div style="display: flex">
            <nobr>
              <span>拒绝原因:</span>
              <el-input
                v-model="refuseReason"
                clearable
                placeholder="请输入拒绝原因"
              />
            </nobr>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button
              type="danger"
              @click="handleApply(row, false, refuseReason)"
            >确 定</el-button>
          </span>
        </el-dialog>
      </div>
    </el-tabs>
  </div>
</template>

<script>
  import TablePagination from '@/components/TablePagination'
  import { handleWithdraw, getWithdrawList } from '@/api/withdraw'
  import { getCountryItem } from '@/filters'

  export default {
    name: 'UnfinishFinancial',
    components: { TablePagination },
    filters: {
      filterType(type) {
        switch (type) {
          case 0:
            return '待审核'
          case 1:
            return '提现失败'
          case 2:
            return '提现成功'
          case 3:
            return '拒绝提现'
          default:
            return '未知类型'
        }
      },
      identify(key) {
        const obj = {
          0: '未认证',
          1: '真人认证',
          2: '主播认证'
        }
        return obj[key]
      },
      forbidden(key) {
        const obj = {
          0: '正常',
          1: '全局封禁',
          2: '禁聊'
        }
        return obj[key]
      }
    },
    data() {
      return {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        defaultQueryParams: {},
        listQuery: {
          page: 1,
          limit: 20,
          status: '0,1',
          startTime: null,
          endTime: null,
          userId: null
        },
        payType: {
          0: '支付宝',
          1: '微信',
          2: 'paypal'
        },
        payAmount: {
          0: '元',
          1: '元',
          2: '美元'
        },
        options: [
          {
            value: false,
            label: '拒绝支付'
          },
          {
            value: true,
            label: '允许支付'
          }
        ],
        countryCodeItem: getCountryItem(),
        daterangeValue: '',
        value: null,
        multipleSelection: undefined,
        refuseReason: '',
        dialogVisible: false,
        dialogLoading: true,
        row: null,
      }
    },
    created() {
      this.doSearch()
    },
    methods: {
      showDialog(row) {
        this.row = row
        this.dialogVisible = true
        this.dialogLoading = false
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleResetFilter() {
        this.defaultQueryParams = Object.assign(
          {},
          {
            page: 1,
            limit: 20,
            status: '0,1',
            startTime: null,
            endTime: null,
            userId: null
          }
        )
        this.value = null
        this.resetFilter()
      },
      handleApply({ withDrawId }, result, refuseReason) {
        this.$confirm('确认是否继续此操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            const p = {
              result,
              refuseReason: refuseReason,
              withDrawId: [withDrawId]
            }
            handleWithdraw(p).then(
              res => {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                })
                this.dialogVisible = false
                this.dialogLoading = false
                this.doSearch()
              },
              rej => {
                this.$alert(`失败原因：${rej.data};`, '提现失败')
              }
            )
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
            this.dialogVisible = false
            this.dialogLoading = false
          })
      },
      handleMutiple() {
        if (!this.multipleSelection || this.value === null) {
          this.$message({
            message: '请选择操作类型以及数据！！！',
            type: 'error'
          })
          return
        } else {
          if (this.value === false) {
            this.showDialog()
          }
          const withDrawId = this.multipleSelection.map(item => item.withDrawId)
          const p = {
            result: this.value,
            refuseReason: this.refuseReason,
            withDrawId
          }
          handleWithdraw(p).then(res => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.dialogVisible = false
            this.dialogLoading = false
            this.doSearch()
          })
        }
      },
      doSearch() {
        this.listLoading = true
        getWithdrawList(this.listQuery).then(response => {
          this.list = response.data.userWithdrawList
          this.total = response.data.total
          this.listLoading = false
        })
      },
      handleClick(tab) {
        switch (tab.name) {
          case 'second':
            this.$router.push('/anchor/withdraw-resolve')
            break
          case 'third':
            this.$router.push('/anchor/withdraw-rejected')
            break
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .dialog-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    > span {
      width: 30%;
      margin: 10px 0;
      > span {
        font-weight: 700;
      }
    }
  }
</style>
